package com.example.waibao.service.impl;

import com.example.waibao.Component.UserHelper;
import com.example.waibao.entity.User;
import com.example.waibao.service.UserService;
import com.example.waibao.service.taskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.*;

@Slf4j
@Component
public class ScheduledService {

    @Autowired
    private UserService userService;

    @Autowired
    private taskService taskService;


//    @Scheduled(cron = "0/18 * * * * ?")
    public void scheduled() throws IOException {


        SseEmitterServer sseEmitterServer = new SseEmitterServer();
        Map<String,Object> returnMap = new HashMap<>();
        //向指定用户发送信息

       // String message = "恭喜获得隐藏头像框";

//        List<String> messages = Arrays.asList("恭喜获得隐藏头像框","恭喜获得等级奖励");
//        String message = messages.get((int)(Math.random()*messages.size()));

        //从在线学生中随机选取
        List<String> students = new ArrayList<>(SseEmitterServer.getIds());
        int id = (int)(Math.random()*students.size());
        int bool = (int)(Math.random()*2);
        if(!students.isEmpty()&&bool==1){

            System.out.println("id = "+Integer.parseInt(students.get(id)));
            int school_id = userService.get_a_school_id(Integer.parseInt(students.get(id)));
            List<String> boxs = taskService.get_box(school_id);
            boxs.add("恭喜获得隐藏头像框");
            boxs.add("恭喜获得等级奖励");
            String message = boxs.get((int)(Math.random()*boxs.size()));
            if (message.equals("恭喜获得隐藏头像框")){
                String name = userService.check_head_have(Integer.parseInt(students.get(id)));
                if (name==null){
                    sseEmitterServer.sendOneClientMessage(String.valueOf(students.get(id)),message);
                    returnMap.put("msg","向id为"+id+"的用户发送："+message+"成功!");
                    returnMap.put("code","200");
                    returnMap.put("data",null);
                }
            }else if(message.equals("恭喜获得等级奖励")){
                sseEmitterServer.sendOneClientMessage(String.valueOf(students.get(id)),message);
            }else {
                message = "特殊奖励:"+message;
                sseEmitterServer.sendOneClientMessage(String.valueOf(students.get(id)),message);
            }

            log.info("=====>>>>>使用cron  {}",System.currentTimeMillis());
        }

        //log.info("=====>>>>>使用cron  {}",System.currentTimeMillis());
    }

//    @Scheduled(fixedRate = 5000)
//    public void scheduled1() {
//        log.info("=====>>>>>使用fixedRate{}", System.currentTimeMillis());
//    }
//
//    @Scheduled(fixedDelay = 5000)
//    public void scheduled2() {
//        log.info("=====>>>>>fixedDelay{}",System.currentTimeMillis());
//    }

}

